package com.hex.liyue.usermodule.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.view.recycleview.YRecycleview;
import com.hex.liyue.usermodule.adapter.AdapterOrder;
import com.hex.liyue.usermodule.bean.MyOrderResultBean;
import com.hex.liyue.usermodule.presenter.IMyOrderView;
import com.hex.liyue.usermodule.presenter.MyOrderPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by WJ on 2016/6/29.
 */
public class MyOrderActivity extends BaseActivity implements IMyOrderView, YRecycleview.RefreshAndLoadMoreListener {
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.rv_order_list)
    YRecycleview rvOrderList;
    @Bind(R.id.empty_order)
    LinearLayout emptyOrder;
    private MyOrderPresenter myOrderPresenter;
    private int page = 1, pagenum = 10;
    private List<MyOrderResultBean.DataBean> list = new ArrayList<>();
    private AdapterOrder adapterOrder;

    @Override
    protected void bindPresenter() {
        myOrderPresenter = new MyOrderPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        myOrderPresenter.orderList(page, pagenum);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_my_order;
    }

    @Override
    protected void initToolbar() {
        ivToobarBack.setVisibility(View.VISIBLE);
        tvToobarTitle.setText("我的订单");
    }


    @OnClick({R.id.iv_toobar_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
        }
    }

    @Override
    public void showLod(String msg) {
        showLoad(this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }



    @Override
    public void setAdapter(MyOrderResultBean myOrderResultBean) {
        if (page == 1) {
            list.clear();
        }
        if (myOrderResultBean != null) {
            if (myOrderResultBean.data.size() == 0 && page > 1) {
                page--;
            }
            for (MyOrderResultBean.DataBean dataBean : myOrderResultBean.data) {
                list.add(dataBean);
            }
        }
        if (adapterOrder == null) {
            adapterOrder = new AdapterOrder();
            adapterOrder.setOnItemClickListener(new AdapterOrder.OnItemClickListener() {
                @Override
                public void onItemClickListener(int position) {
                    //行点击
                    Intent intent = new Intent(MyOrderActivity.this, OrderDetailsActivity.class);
                    intent.putExtra("orderId", list.get(position).order_id);
                    startActivityForResult(intent, 0);
                }
            });
            rvOrderList.setLayoutManager(new LinearLayoutManager(this));
            rvOrderList.setRefreshAndLoadMoreListener(this);
            rvOrderList.setEmptyView(emptyOrder);
            rvOrderList.setAdapter(adapterOrder);
        }
        adapterOrder.setList(list);
        adapterOrder.notifyDataSetChanged();
        rvOrderList.reSetStatus();
        L.d("WJ", page + "=size=" + myOrderResultBean.data.size());
    }

    @Override
    public void onRefresh() {
        page = 1;
        myOrderPresenter.orderList(page, pagenum);
    }

    @Override
    public void onLoadMore() {
        page++;
        myOrderPresenter.orderList(page, pagenum);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == RESULT_OK) {
            //从订单详情到了订单列表
            L.d("WJ", "从订单详情到了订单列表 刷新列表");
            myOrderPresenter.orderList(page, pagenum);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
